package itsos.service;



import java.util.Collection;

import java.util.Iterator;

import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;


public class DisplayRepositoryTree {
   
    public static void main(String[] args) {
       
        String url = "http://svn.svnkit.com/repos/svnkit/tags/";
        String name = "anymous";
        String password = "anymous";

        
        setupLibrary();
        
        SVNRepository repository = null;
        
        repository = UrlReg(repository, url);

        
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
        repository.setAuthenticationManager(authManager);
        
        InspectUrl(repository, url);
        
        PrintRevision(repository);
        
    }
    
    public static void PrintRevision(SVNRepository repository)
    {
    	long latestRevision = -1;
        try {
            latestRevision = repository.getLatestRevision();
        } catch (SVNException svne) {
            System.err
                    .println("error while fetching the latest repository revision: "
                            + svne.getMessage());
//            System.exit(1);
        }
        System.out.println("");
        System.out.println("---------------------------------------------");
        System.out.println("Repository latest revision: " + latestRevision);
//        System.exit(0);
    }

    
    public static void InspectUrl(SVNRepository repository, String url)
    {
    		try {
            
            SVNNodeKind nodeKind = repository.checkPath("", -1);
            if (nodeKind == SVNNodeKind.NONE) {
                System.err.println("There is no entry at '" + url + "'.");
//                System.exit(1);
            } else if (nodeKind == SVNNodeKind.FILE) {
                System.err.println("The entry at '" + url + "' is a file while a directory was expected.");
//                System.exit(1);
            }
            
            System.out.println("Repository Root: " + repository.getRepositoryRoot(true));
            
            System.out.println("Repository UUID: " + repository.getRepositoryUUID(true));
            System.out.println("");

            
            
            
        } catch (SVNException svne) {
            System.err.println("error while listing entries: "
                    + svne.getMessage());
//            System.exit(1);
        }
    	
    }
    
    
    
    
    public static SVNRepository UrlReg(SVNRepository repository ,String url)
    {
    	try {
            
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        } catch (SVNException svne) {
           
            System.err
                    .println("error while creating an SVNRepository for location '"
                            + url + "': " + svne.getMessage());
            //System.exit(1);
        }
    	
    	return repository;
    }
    
    public static void setupLibrary() {
        
        DAVRepositoryFactory.setup();
        
        SVNRepositoryFactoryImpl.setup();
        
       
        FSRepositoryFactory.setup();
    }

    
    public static void listEntries(SVNRepository repository, String path)
            throws SVNException {
        
        Collection entries = repository.getDir(path, -1, null,
                (Collection) null);
        Iterator iterator = entries.iterator();
        
        
        
        
       while (iterator.hasNext()) {
            
            SVNDirEntry entry = (SVNDirEntry) iterator.next();
            if(entry.getKind() == SVNNodeKind.DIR)
            {
            
            System.out.println("/" + entry.getName());
            }
        }
       
        
    }
    public static Collection listEntry(SVNRepository repository, String path)
    throws SVNException
    {
    	
        Collection entries = repository.getDir(path, -1, null,
                (Collection) null);
            	
       return entries;
    	
    	
    }
    
    
    public static Iterator FullPac(String url, String name, String password) throws SVNException
    {
    	SVNRepository repository = null;
    	Collection collect = null;
    	Iterator iterater = null;
    	setupLibrary();
    	repository = UrlReg(repository, url);
    	ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
    	repository.setAuthenticationManager(authManager);
    	InspectUrl(repository, url);
    	collect = listEntry(repository, "");
    	iterater = collect.iterator();
    	return iterater;
    }
    
    public static int NumNex(int a)
    {
    	return a++;
    }
    
    
    
    
}